﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace QnD_Image_comparer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public Bitmap image1;
        public Bitmap image2;

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK && File.Exists( ofd.FileName))
            {
                try
                {
                    image1 = new Bitmap(ofd.FileName);
                    linkLabel1.Text = ofd.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + "\nAre you sure it was a bitmap? (PNG, JPG or BMP)", "Failed to load bitmap from file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   
                }
                
            }
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK && File.Exists(ofd.FileName))
            {
                try
                {
                    image2 = new Bitmap(ofd.FileName);
                    linkLabel2.Text = ofd.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + "\nAre you sure it was a bitmap? (PNG, JPG or BMP)", "Failed to load bitmap from file", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }
        }

        public Bitmap resultimage;
        private void button1_Click(object sender, EventArgs e)
        {
            if (image1.Size.Height < image2.Size.Height || image1.Size.Width < image2.Size.Width || image1.Size.Height > image2.Size.Height || image1.Size.Width > image2.Size.Width)
                MessageBox.Show("Image dimension error!\nimages have to be of the same size to compare.", "Error.", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                Thread processor = new Thread(new ThreadStart(processimage));
                CheckForIllegalCrossThreadCalls = false;
                processor.Start();
                label1.Text = "Processing..";
                button1.Enabled = false;
            }

        }

        public void processimage()
        {
            resultimage = image1;
            progressBar1.Maximum = image1.Width;
            for (int x1 = 0; x1 < image1.Width; x1++)
            {
                
                progressBar1.Value = x1;
                for (int y1 = 0; y1 < image1.Height; y1++)
                {
                    if (!(image1.GetPixel(x1, y1) == image2.GetPixel(x1, y1)))
                    {
                        resultimage.SetPixel(x1, y1, Color.DarkViolet);
                    }
                }
            }
            label1.Text = "Done!";
            pictureBox1.Image = resultimage;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            button1.Enabled = true;
            button2.Enabled = true;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.Filter = "PNG|*.png";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                resultimage.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Png);
            }
        }
    }
}
